/*
 * 文件名：TwoQueueStack.java
 * 描述： TwoQueueStack.java
 * 修改人：Lo&Wang
 * 修改时间：2016年11月28日
 * 修改内容：新增
 */
package com.lowang.test.queue;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

/**
 * @author zhengmo
 */
public class TwoQueueStack {
    private BlockingQueue<Integer> pushQueue = new ArrayBlockingQueue<>(50);
    private BlockingQueue<Integer> popQueue = new ArrayBlockingQueue<>(50);
    public TwoQueueStack() {
    }
    public void push(Integer num) throws InterruptedException {
        pushQueue.put(num);
    }
    public Integer pop() throws InterruptedException {
        if (pushQueue.isEmpty() && popQueue.isEmpty()) {
            return null;
        }
        if (pushQueue.size() > 0) {
            while (pushQueue.size() != 1) {
                popQueue.put(pushQueue.poll());
            }
            return pushQueue.poll();
        }
        if (popQueue.size() > 0) {
            while (popQueue.size() != 1) {
                pushQueue.put(popQueue.poll());
            }
        }
        return popQueue.poll();
    }
    public boolean isEmpty() {
        return pushQueue.isEmpty() && popQueue.isEmpty();
    }
    public static void main(String[] args) throws InterruptedException {
        TwoQueueStack stack = new TwoQueueStack();
        stack.push(1);
        stack.push(4);
        stack.push(2);
        stack.push(0);
        while (!stack.isEmpty()) {
            System.out.println(stack.pop());
        }
    }
}
